<template>
    <el-dialog
        :title="name ? '编辑分类' : '新增分类'"
        :visible.sync="show"
        @open="_open"
        :append-to-body="true"
        width="30%">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
            <el-form-item label="分类名称" prop="name" required>
                <el-input v-model="form.name"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="close">取消</el-button>
            <el-button type="primary" @click="save">保存</el-button>
        </span>
    </el-dialog>
</template>

<script lang="ts">
import { Component, Vue } from "vue-property-decorator";

@Component
export default class MyName extends Vue {
    form = {
        name: ""
    }

    name = ""

    show = false

    rules = {
        name: [
            { required: true, message: '请输入分类名称', trigger: ['blur', 'change'] },
            { max: 10, message: "最大输入10个字符", trigger: 'blur' }
        ]
    }

    _open() {
        this.form.name = this.name;
    }

    save() {
        (this.$refs.form as any).validate((valid: any) => {
            if (valid) {
                this.$emit("sure", this.form.name);
            }
        });
    }

    close() {
        (this.$refs.form as any).resetFields();
        this.show = false;
    }
}
</script>

<style lang="scss" scoped>

</style>
